package com.canbot.u05.activity.editmodule;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.AppInfo;

import java.util.ArrayList;
import java.util.List;

public class AppInfoUnSelctAdapter extends RecyclerView.Adapter<AppInfoUnSelctAdapter.Vh>{
    private static final String TAG = "AppInfoUnSelctAdapter";
    private final String[] orderModuleNames;
    private final TypedArray drawableArray;
    private int layoutId;

    private Context context;
        public ArrayList<AppInfo> appInfos;


    private AppInfoSelctAdapter selectAdapter;


    public AppInfoUnSelctAdapter(Context context, ArrayList<AppInfo> appInfos,int layoutId) {
            this.context = context;
            this.appInfos = appInfos;
            orderModuleNames = context.getResources().getStringArray(R.array.order_edit_module_names);
            drawableArray = context.getResources().obtainTypedArray(R.array.order_edit_module_drawables);
            this.layoutId = layoutId;
    }

        public void setSelectAdapter(AppInfoSelctAdapter selectAdapter){

            this.selectAdapter = selectAdapter;
        }

        public void setDatas(ArrayList<AppInfo> appInfos){
            this.appInfos = appInfos;
            notifyDataSetChanged();
        }


        public ArrayList<AppInfo> getdatas(){
            return appInfos;
        }

        @Override
        public AppInfoUnSelctAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Vh(LayoutInflater.from(context).inflate(layoutId,null));
        }

        @Override
        public void onBindViewHolder(AppInfoUnSelctAdapter.Vh holder, final int position) {
             AppInfo appInfo = appInfos.get(position);
             String nickname = appInfo.getNickname();
             String appName = appInfo.getAppName();
             String showName = TextUtils.isEmpty(nickname) ? appName : nickname;

             holder.tv.setText(showName);

             int orderIndex = calculateOrderIndex(appName);

             Log.e(TAG,"orderIndex:" + orderIndex);

             Drawable drawable = context.getResources().getDrawable(drawableArray.getResourceId(orderIndex, 0));

             holder.iv.setImageDrawable(drawable);

             holder.itemView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     remove(position);
                 }
             });
        }


        private int calculateOrderIndex(String name) {
            for (int i = 0; i < orderModuleNames.length; i++) {
                if (name.equals(orderModuleNames[i])){
                    return i;
                }
            }

            return 0;
        }

        @Override
        public int getItemCount() {

            return appInfos== null ? 0 : appInfos.size();
        }

        public void add(AppInfo item){
            int position = appInfos.size();
            appInfos.add(item);
            notifyItemInserted(position);
        }

        public void add(int position,AppInfo item){
            appInfos.add(position,item);
            notifyItemInserted(position);
        }

    /**
     * 添加至选中列表（我的功能）再删除未选中列表（其他功能）中的数据
     * @param position
     */
    public void remove(int position) {

            if (selectAdapter != null){
                AppInfo info = appInfos.get(position);
                info.setMainFunction(true);
                selectAdapter.add(info);
                notifyItemInserted(selectAdapter.getItemCount());
            }

            appInfos.remove(position);

            notifyDataSetChanged();
        }

        class Vh extends RecyclerView.ViewHolder {

            public Vh(View itemView) {
                super(itemView);
                this.itemView = itemView;
                tv = itemView.findViewById(R.id.tv);
                ivDelete = itemView.findViewById(R.id.iv_delete);
                iv = itemView.findViewById(R.id.iv);
            }

            public TextView tv;
            public ImageView ivDelete;
            public ImageView iv;
            public View itemView;
        }
    }